/*
 * @lc app=leetcode.cn id=102 lang=javascript
 *
 * [102] 二叉树的层序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
    if (!root) return []
    let stack = [root], ans = []
    while (stack.length > 0) {
        let nodes = stack.splice(0)
        let el = []
        nodes.forEach(node => {
            if (node.left) stack.push(node.left)
            if (node.right) stack.push(node.right)
            el.push(node.val)
        })
        ans.push(el)
    }
    return ans
};
// @lc code=end

